package com.zkyc.framework.website.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.toolkit.SqlHelper;
import com.zkyc.framework.common.exception.ResultCode;
import com.zkyc.framework.common.mapper.PostMapper;
import com.zkyc.framework.common.pojo.Post;
import com.zkyc.framework.common.utils.SqlConditionUtil;
import com.zkyc.framework.common.vo.ResultVO;
import com.zkyc.framework.website.service.PostService;
import com.zkyc.framework.website.vo.PostPageVo;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;

import javax.annotation.Resource;
import java.util.Arrays;
import java.util.HashSet;
import java.util.List;

/**
 * (Post)表服务实现类
 *
 * @author libingchuan
 * @since 2022-10-11 10:58:31
 */
@Service("postService")
public class PostServiceImpl implements PostService {
    @Resource
    private PostMapper postMapper;

    public static final int PostSize = 5;

    /**
     * 通过ID查询单条数据
     *
     * @param id 主键
     * @return 实例对象
     */
    @Override
    public Post queryById(Integer id) {
        return this.postMapper.selectById(id);
    }

    /**
     * 分页查询
     *
     * @return 查询结果
     */
    @Override
    public ResultVO queryByPage(PostPageVo postPageVo) throws Exception {
        if (postPageVo == null) {
            return ResultVO.fail(ResultCode.PARAM_INVALID);
        }
        postPageVo.checkParam();
        Page page = new Page(postPageVo.getPage(), postPageVo.getSize());
        String postName = postPageVo.getPostName();
        postPageVo.setPostName(null);
        QueryWrapper<Post> queryWrapper = (QueryWrapper<Post>) SqlConditionUtil.getQueryWrapper(SqlHelper.table(Post.class).getTableName(), postPageVo, new QueryWrapper());
        queryWrapper.eq("languages", postPageVo.getLanguages());
        if (StringUtils.isNotBlank(postName)) {
            queryWrapper.and(wrapper -> wrapper.like("post_name", postName).or().like("description", postName).or().like("request", postName));
        }
        queryWrapper.orderByDesc("publish_date");
        IPage<Post> page1 = postMapper.selectPage(page, queryWrapper);
        return ResultVO.data(page1);
    }

    /**
     * 查询
     *
     * @return 查询结果
     * @param languages
     */
    @Override
    public ResultVO<?> queryList(Integer languages) {
        LambdaQueryWrapper<Post> postLqw = new LambdaQueryWrapper<>();
        postLqw.eq(Post::getLanguages, languages).orderByDesc(Post::getPublishDate).last("limit " + PostSize);
        List<Post> postList = postMapper.selectList(postLqw);
        return ResultVO.data(postList);
    }

    @Override
    public ResultVO<?> getHeader(Integer languages) {
        List<Post> posts = postMapper.selectList(new LambdaQueryWrapper<Post>().eq(Post::getLanguages, languages));
        HashSet<String> city = new HashSet<>();
        HashSet<String> orgName = new HashSet<>();
        for (Post post : posts) {
            String place = post.getPlace();
            String[] split = place.split(",");
            city.addAll(Arrays.asList(split));
            orgName.add((post.getResearchCenter()));
        }
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("city",city);
        jsonObject.put("orgName",orgName);
        return ResultVO.data(jsonObject);
    }


    /**
     * 新增数据
     *
     * @param post 实例对象
     * @return 实例对象
     */
    @Override
    public ResultVO insert(Post post) {
        if (post == null) {
            return ResultVO.fail(ResultCode.PARAM_INVALID);
        }
        postMapper.insert(post);
        return ResultVO.success(ResultCode.SUCCESS);
    }

    /**
     * 修改数据
     *
     * @param post 实例对象
     * @return 实例对象
     */
    @Override
    public ResultVO update(Post post) {
        if (post == null) {
            return ResultVO.fail(ResultCode.PARAM_INVALID);
        }
        if (queryById(post.getId()) == null) {
            return ResultVO.fail(ResultCode.SERVE_ERROR);
        }
        postMapper.updateById(post);
        return ResultVO.success(ResultCode.SUCCESS);
    }

    /**
     * 通过主键删除数据
     *
     * @param id 主键
     * @return 是否成功
     */
    @Override
    public ResultVO deleteById(Integer id) {
        if (id == null) {
            return ResultVO.fail(ResultCode.PARAM_INVALID);
        }
        if (queryById(id) == null) {
            return ResultVO.fail(ResultCode.SERVE_ERROR);
        }
        postMapper.deleteById(id);
        return ResultVO.success(ResultCode.SUCCESS);
    }

}
